package com.logic.landseaserver.service.impl;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.LandeaConstants.AppropriateType;
import com.logic.landseaserver.common.LandeaConstants.ApprovalStatus;
import com.logic.landseaserver.common.LandeaConstants.CodeType;
import com.logic.landseaserver.common.LandeaConstants.CouponStatus;
import com.logic.landseaserver.common.LandeaConstants.FeeType;
import com.logic.landseaserver.common.LandeaConstants.MoneyType;
import com.logic.landseaserver.common.LandeaConstants.OfferType;
import com.logic.landseaserver.common.VariableChar;
import com.logic.landseaserver.common.enums.BillTypeEnum;
import com.logic.landseaserver.common.enums.DepositEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.AuthorizationResultCode;
import com.logic.landseaserver.common.util.Arith;
import com.logic.landseaserver.common.util.DateUtil;
import com.logic.landseaserver.domain.Coupon;
import com.logic.landseaserver.domain.CouponPutRecord;
import com.logic.landseaserver.domain.Project;
import com.logic.landseaserver.persistence.read.BalanceSheetRecordReadMapper;
import com.logic.landseaserver.persistence.read.CoupUsedRecordReadMapper;
import com.logic.landseaserver.persistence.read.CouponPutRecordReadMapper;
import com.logic.landseaserver.persistence.read.CouponReadMapper;
import com.logic.landseaserver.persistence.read.CustomerReadMapper;
import com.logic.landseaserver.persistence.read.CustomerVersionReadMapper;
import com.logic.landseaserver.persistence.read.ExpensesReadMapper;
import com.logic.landseaserver.persistence.read.ProjectReadMapper;
import com.logic.landseaserver.persistence.write.CouponPutRecordWriteMapper;
import com.logic.landseaserver.persistence.write.CouponWriteMapper;
import com.logic.landseaserver.service.IAuthorizationCodeService;
import com.logic.landseaserver.ws.dto.CouponDTO;
import com.logic.landseaserver.ws.dto.CouponGeneralDTO;
import com.logic.landseaserver.ws.dto.CouponPersonalDTO;
import com.logic.landseaserver.ws.request.CouponPutRecordReq;
import com.logic.landseaserver.ws.request.CustomerConditionReq;
import com.logic.landseaserver.ws.response.AuthorizedItemResp;
import com.logic.landseaserver.ws.response.CouponPutRecordResp;
import com.logic.landseaserver.ws.response.CouponUsedResp;
import com.logic.landseaserver.ws.response.EffectiveTimeResp;
import com.logic.landseaserver.ws.response.ExpenseTypeResp;
import com.logic.landseaserver.ws.response.ProjectResp;
import com.logic.landseaserver.ws.response.RoomResp;
import com.logic.landseaserver.ws.response.TenantDetailResp;
import com.logic.landseaserver.ws.response.WeChatAuthCodeResp;
import com.logic.system.persistence.read.CodeReadMapper;
import com.logic.system.persistence.read.ConfigurationReadMapper;


/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：AuthorizationServiceImpl</br>
 * 类描述：</br>
 * 创建人：Crystal</br>
 * 创建时间：2017年9月7日 下午1:50:50</br>
 * @version 1.0
 *
 */
@Service
public class AuthorizationServiceImpl implements IAuthorizationCodeService
{
	private static final Logger LOG = LoggerFactory.getLogger(AuthorizationServiceImpl.class);

	@Autowired
	private CustomerReadMapper customerReadMapper;

	@Autowired
	private ProjectReadMapper projectReadMapper;

	@Autowired
	private ExpensesReadMapper expensesReadMapper;

	@Autowired
	private CouponWriteMapper couponWriteMapper;

	@Autowired
	private CouponReadMapper couponReadMapper;

	@Autowired
	private ConfigurationReadMapper configurationReadMapper;

	@Autowired
	private CouponPutRecordReadMapper couponPutRecordReadMapper;

	@Autowired
	private  CouponPutRecordWriteMapper couponPutRecordWriteMapper;

	@Autowired
	private  CoupUsedRecordReadMapper coupUsedRecordReadMapper;

	@Autowired
	private  BalanceSheetRecordReadMapper balanceSheetRecordReadMapper;

	@Autowired
	private CustomerVersionReadMapper customerVersionReadMapper;

	@Autowired
	private CodeReadMapper codeReadMapper;

	@Override
	public TenantDetailResp queryTenantDetail(CustomerConditionReq conditionReq)throws LandseaException
	{
		List<TenantDetailResp> resps = new ArrayList<TenantDetailResp>();
		TenantDetailResp resultResp =  new TenantDetailResp();
		try
		{
			List<RoomResp> roomList = new ArrayList<RoomResp>();
			//退转换续类
			if (CodeType.RELIEFTYPE.equals(conditionReq.getCodeType()))
			{               
				if (null != conditionReq.getProjectIds())
				{   //租客有在正确的门店没有签了合同            
					String projectSql = "and lco.project_id IN ("+conditionReq.getProjectIds()+") "; 
					resps = customerReadMapper.queryTenantDetail(conditionReq.getPhone(),projectSql);                  
					if (CollectionUtils.isEmpty(resps) )
					{
						resultResp = customerReadMapper.queryTenantDetailByPhone(conditionReq.getPhone());
					} 
					else
					{
						for (TenantDetailResp tenantDetailResp : resps)
						{
							RoomResp resp = new RoomResp();
							resp.setRoomId(tenantDetailResp.getRoomId());
							resp.setRoomName(tenantDetailResp.getRoomName());
							roomList.add(resp);
							resultResp.setName(tenantDetailResp.getName());
							resultResp.setUserId(tenantDetailResp.getUserId());
							resultResp.setRoomIdList(roomList);
						}
						resultResp.setRoomId(null);
						resultResp.setRoomName(null);
					}
				}
				else
				{
					List<String> areas = new ArrayList<String>();
					String [] arry = conditionReq.getAreaIds().split(",");
					for (String areaId : arry)
					{
						areas.add(areaId);
					}
					String areaSql = buildAreaCondition(areas);
					resps = customerReadMapper.queryTenantDetailByArea(areaSql,conditionReq.getPhone());
					if (CollectionUtils.isEmpty(resps) )
					{
						resultResp = customerReadMapper.queryTenantDetailByPhone(conditionReq.getPhone());
					}
					else
					{
						for (TenantDetailResp tenantDetailResp : resps)
						{
							RoomResp resp = new RoomResp();
							resp.setRoomId(tenantDetailResp.getRoomId());
							resp.setRoomName(tenantDetailResp.getRoomName());
							roomList.add(resp);
							resultResp.setName(tenantDetailResp.getName());
							resultResp.setUserId(tenantDetailResp.getUserId());
						}
						resultResp.setRoomIdList(roomList);
						resultResp.setRoomId(null);
						resultResp.setRoomName(null);   
					}

				}
			}
			else
			{
				resultResp= customerReadMapper.queryTenantDetailByPhone(conditionReq.getPhone()); 
			}
		}

		catch (Exception e)
		{

		}   
		return resultResp;
	}

	private String buildAreaCondition(List<String> areas)
	{
		if (CollectionUtils.isEmpty(areas))
		{
			return null;
		}       
		StringBuilder stringBuilder = new StringBuilder();        
		stringBuilder.append(" and  (");        
		int size = areas.size();       
		for (int i = 0 ; i < size; i ++)
		{
			stringBuilder.append("lco.project_id IN (select id from ls_project where area_code = '");
			stringBuilder.append(areas.get(i));
			stringBuilder.append("' )");

			if (i != size - 1)
			{
				stringBuilder.append(" or ");
			}
		}
		stringBuilder.append(")");        
		return stringBuilder.toString();
	}

	@Override
	public Map<String, Object> getAuthorizedItem()
	{
		Map<String,Object> respMap = new HashMap<String,Object>();
		//授权项目
		List<AuthorizedItemResp> authorizedProjectResps = projectReadMapper.selectProjectAndCity(SecurityUtils.getCurrentLogin().getUserId());
		List<Map<String, Object>> projectList = new ArrayList<Map<String, Object>>();
		//授权区域
		List<AuthorizedItemResp> authorizedAreaResps = projectReadMapper.selectAreaByUserId(SecurityUtils.getCurrentLogin().getUserId());
		for (AuthorizedItemResp authorizedAreaResp : authorizedAreaResps)
		{
			authorizedProjectResps.add(authorizedAreaResp);
		}
		for (AuthorizedItemResp authorizedItemResp : authorizedProjectResps)
		{           
			Map<String, Object> projectMap = new HashMap<String,Object>();
			projectMap.put("projectInfo", authorizedItemResp);
			projectList.add(projectMap);
		}
		respMap.put("projectList", projectList);

		//月结的code值
		List<ExpenseTypeResp> monthlyBillCreates = expensesReadMapper.selectFee(FeeType.MONTHLYBILLCREATED);
		respMap.put("monthlyBillCreates", monthlyBillCreates);

		//按签约使用的月租金和服务费押金定金
		List<ExpenseTypeResp> feeCreates = new ArrayList<ExpenseTypeResp>();
		ExpenseTypeResp feeExpenseTypeResp = new ExpenseTypeResp();
		feeExpenseTypeResp.setTypeCode(BillTypeEnum.CTBI9.getCode());
		feeExpenseTypeResp.setTypeName(BillTypeEnum.CTBI9.getName());
		ExpenseTypeResp feeExpenseTypeResp2 = new ExpenseTypeResp();
		feeExpenseTypeResp2.setTypeCode(BillTypeEnum.CTBI8.getCode());
		feeExpenseTypeResp2.setTypeName(BillTypeEnum.CTBI8.getName());

		ExpenseTypeResp feeExpenseTypeResp3 = new ExpenseTypeResp();
		feeExpenseTypeResp3.setTypeCode(BillTypeEnum.CTBI11.getCode());
		feeExpenseTypeResp3.setTypeName(BillTypeEnum.CTBI11.getName());
		ExpenseTypeResp feeExpenseTypeResp4 = new ExpenseTypeResp();
		feeExpenseTypeResp4.setTypeCode(BillTypeEnum.CTBI18.getCode());
		feeExpenseTypeResp4.setTypeName(BillTypeEnum.CTBI18.getName());
		ExpenseTypeResp feeExpenseTypeResp5 = new ExpenseTypeResp();
		feeExpenseTypeResp5.setTypeCode(BillTypeEnum.CTBI10.getCode());
		feeExpenseTypeResp5.setTypeName(BillTypeEnum.CTBI10.getName());
		feeCreates.add(feeExpenseTypeResp5);
		feeCreates.add(feeExpenseTypeResp2);
		feeCreates.add(feeExpenseTypeResp);
		feeCreates.add(feeExpenseTypeResp3);
		feeCreates.add(feeExpenseTypeResp4);
		respMap.put("feeCreates", feeCreates);

		//区域月结账单费项
		List<ExpenseTypeResp> monthlyBillFees = projectReadMapper.selectFeeForMonth();

		ExpenseTypeResp expenseTypeResp1 = new ExpenseTypeResp();
		expenseTypeResp1.setTypeCode(BillTypeEnum.CTBI8.getCode());
		expenseTypeResp1.setTypeName(BillTypeEnum.CTBI8.getName());
		//monthlyBillFees.add(expenseTypeResp1);
		ExpenseTypeResp expenseTypeResp2 = new ExpenseTypeResp();
		expenseTypeResp2.setTypeCode(BillTypeEnum.CTBI9.getCode());
		expenseTypeResp2.setTypeName(BillTypeEnum.CTBI9.getName());
		//monthlyBillFees.add(expenseTypeResp2);
		ExpenseTypeResp expenseTypeResp4 = new ExpenseTypeResp();
		expenseTypeResp4.setTypeCode(BillTypeEnum.CTBI2.getCode());
		expenseTypeResp4.setTypeName(BillTypeEnum.CTBI2.getName());
		//monthlyBillFees.add(expenseTypeResp4);
		ExpenseTypeResp expenseTypeResp5 = new ExpenseTypeResp();
		expenseTypeResp5.setTypeCode(BillTypeEnum.CTBI1.getCode());
		expenseTypeResp5.setTypeName(BillTypeEnum.CTBI1.getName());
		//monthlyBillFees.add(expenseTypeResp5);
		ExpenseTypeResp expenseTypeResp6 = new ExpenseTypeResp();
		expenseTypeResp6.setTypeCode(BillTypeEnum.CTBI3.getCode());
		expenseTypeResp6.setTypeName(BillTypeEnum.CTBI3.getName());
		//monthlyBillFees.add(expenseTypeResp6);
		ExpenseTypeResp expenseTypeResp7 = new ExpenseTypeResp();
		expenseTypeResp7.setTypeCode(BillTypeEnum.CTBI5.getCode());
		expenseTypeResp7.setTypeName(BillTypeEnum.CTBI5.getName());
		//monthlyBillFees.add(expenseTypeResp7);
		ExpenseTypeResp expenseTypeResp8= new ExpenseTypeResp();
		expenseTypeResp8.setTypeCode(BillTypeEnum.CTBI6.getCode());
		expenseTypeResp8.setTypeName(BillTypeEnum.CTBI6.getName());
		//monthlyBillFees.add(expenseTypeResp8);
		ExpenseTypeResp expenseTypeResp= new ExpenseTypeResp();
		expenseTypeResp.setTypeCode(BillTypeEnum.CTBI13.getCode());
		expenseTypeResp.setTypeName(BillTypeEnum.CTBI13.getName());
		//monthlyBillFees.add(expenseTypeResp);
		ExpenseTypeResp expenseTypeResp9= new ExpenseTypeResp();
		expenseTypeResp9.setTypeCode(BillTypeEnum.CTBI4.getCode());
		expenseTypeResp9.setTypeName(BillTypeEnum.CTBI4.getName());
		//monthlyBillFees.add(expenseTypeResp9);
		ExpenseTypeResp expenseTypeResp10= new ExpenseTypeResp();
		expenseTypeResp10.setTypeCode(BillTypeEnum.CTBI7.getCode());
		expenseTypeResp10.setTypeName(BillTypeEnum.CTBI7.getName());
		//monthlyBillFees.add(expenseTypeResp10);
		ExpenseTypeResp expenseTypeResp11= new ExpenseTypeResp();
		expenseTypeResp11.setTypeCode(BillTypeEnum.CTBI19.getCode());
		expenseTypeResp11.setTypeName(BillTypeEnum.CTBI19.getName());
		//monthlyBillFees.add(expenseTypeResp11);
		respMap.put("monthlyBillFees", monthlyBillFees);

		//退转换续类费项
		List<ExpenseTypeResp> reliefFees = new ArrayList<>();
		ExpenseTypeResp resp2 = new ExpenseTypeResp();
		resp2.setTypeCode(BillTypeEnum.CTBI13.getCode());
		resp2.setTypeName(BillTypeEnum.CTBI13.getName());
		reliefFees.add(resp2);
		ExpenseTypeResp resp4 = new ExpenseTypeResp();
		resp4.setTypeCode(BillTypeEnum.CTBI14.getCode());
		resp4.setTypeName(BillTypeEnum.CTBI14.getName());
		reliefFees.add(resp4);
		ExpenseTypeResp resp5 = new ExpenseTypeResp();
		resp5.setTypeCode(BillTypeEnum.CTBI19.getCode());
		resp5.setTypeName(BillTypeEnum.CTBI19.getName());
		reliefFees.add(resp5);
		ExpenseTypeResp resp6 = new ExpenseTypeResp();
		resp6.setTypeCode(BillTypeEnum.CTBI20.getCode());
		resp6.setTypeName(BillTypeEnum.CTBI20.getName());
		reliefFees.add(resp6);
		ExpenseTypeResp resp7 = new ExpenseTypeResp();
		resp7.setTypeCode(BillTypeEnum.CTBI21.getCode());
		resp7.setTypeName(BillTypeEnum.CTBI21.getName());
		reliefFees.add(resp7);
		reliefFees.add(expenseTypeResp1);
		reliefFees.add(expenseTypeResp2);
		reliefFees.add(expenseTypeResp4);
		reliefFees.add(expenseTypeResp5);
		reliefFees.add(expenseTypeResp6);
		reliefFees.add(expenseTypeResp7);
		reliefFees.add(expenseTypeResp8);
		reliefFees.add(expenseTypeResp9);
		respMap.put("reliefFees", reliefFees);

		//状态
		String statusCode = "ASTATUS";
		List<ExpenseTypeResp> statusLists = expensesReadMapper.selectFee(statusCode);
		respMap.put("statusLists", statusLists);

		//有效时间
		List<EffectiveTimeResp> effectiveTimes = configurationReadMapper.selectEffectiveTime();
		for (EffectiveTimeResp effectiveTimeResp : effectiveTimes)
		{
			String code = effectiveTimeResp.getCode().substring(30);
			effectiveTimeResp.setCode(code);
		}
		respMap.put("effectiveTimes", effectiveTimes);
		respMap.put("nowDate", DateUtil.convertDateTime(new Date()));

		EffectiveTimeResp monthlyEffectiveTime = new EffectiveTimeResp();
		for (EffectiveTimeResp effectiveTimeResp : effectiveTimes)
		{
			if (FeeType.MONTHLYBILLCREATED.equals(effectiveTimeResp.getCode()))
			{
				monthlyEffectiveTime = effectiveTimeResp;
			}
		}
		respMap.put("monthlyEffectiveTime", monthlyEffectiveTime);
		return respMap;
	}

	//给列表用的
	@Override
	public Map<String,Object> getAuthorizedFees()
	{
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//状态
		String statusCode = "ASTATUS";
		List<ExpenseTypeResp> statusLists = expensesReadMapper.selectFee(statusCode);
		resultMap.put("statusLists", statusLists);
		//退转换续类费项
		List<ExpenseTypeResp> expenseFees = new ArrayList<>();
		ExpenseTypeResp resp2 = new ExpenseTypeResp();
		resp2.setTypeCode(BillTypeEnum.CTBI13.getCode());
		resp2.setTypeName(BillTypeEnum.CTBI13.getName());
		expenseFees.add(resp2);
		ExpenseTypeResp resp4 = new ExpenseTypeResp();
		resp4.setTypeCode(BillTypeEnum.CTBI14.getCode());
		resp4.setTypeName(BillTypeEnum.CTBI14.getName());
		expenseFees.add(resp4);
		ExpenseTypeResp resp5 = new ExpenseTypeResp();
		resp5.setTypeCode(BillTypeEnum.CTBI19.getCode());
		resp5.setTypeName(BillTypeEnum.CTBI19.getName());
		expenseFees.add(resp5);
		ExpenseTypeResp resp6 = new ExpenseTypeResp();
		resp6.setTypeCode(BillTypeEnum.CTBI20.getCode());
		resp6.setTypeName(BillTypeEnum.CTBI20.getName());
		expenseFees.add(resp6);
		ExpenseTypeResp resp7 = new ExpenseTypeResp();
		resp7.setTypeCode(BillTypeEnum.CTBI21.getCode());
		resp7.setTypeName(BillTypeEnum.CTBI21.getName());
		expenseFees.add(resp7);
		//按签约使用的月租金和服务费押金定金
		ExpenseTypeResp feeExpenseTypeResp = new ExpenseTypeResp();
		feeExpenseTypeResp.setTypeCode(BillTypeEnum.CTBI9.getCode());
		feeExpenseTypeResp.setTypeName(BillTypeEnum.CTBI9.getName());
		ExpenseTypeResp feeExpenseTypeResp2 = new ExpenseTypeResp();
		feeExpenseTypeResp2.setTypeCode(BillTypeEnum.CTBI8.getCode());
		feeExpenseTypeResp2.setTypeName(BillTypeEnum.CTBI8.getName());
		ExpenseTypeResp feeExpenseTypeResp3 = new ExpenseTypeResp();
		feeExpenseTypeResp3.setTypeCode(BillTypeEnum.CTBI11.getCode());
		feeExpenseTypeResp3.setTypeName(BillTypeEnum.CTBI11.getName());
		ExpenseTypeResp feeExpenseTypeResp4 = new ExpenseTypeResp();
		feeExpenseTypeResp4.setTypeCode(BillTypeEnum.CTBI18.getCode());
		feeExpenseTypeResp4.setTypeName(BillTypeEnum.CTBI18.getName());
		ExpenseTypeResp feeExpenseTypeResp5 = new ExpenseTypeResp();
		feeExpenseTypeResp5.setTypeCode(BillTypeEnum.CTBI10.getCode());
		feeExpenseTypeResp5.setTypeName(BillTypeEnum.CTBI10.getName());
		expenseFees.add(feeExpenseTypeResp2);
		expenseFees.add(feeExpenseTypeResp);
		expenseFees.add(feeExpenseTypeResp3);
		expenseFees.add(feeExpenseTypeResp4);
		expenseFees.add(feeExpenseTypeResp5);

		//区域月结账单费项
		ExpenseTypeResp expenseTypeResp4 = new ExpenseTypeResp();
		expenseTypeResp4.setTypeCode(BillTypeEnum.CTBI2.getCode());
		expenseTypeResp4.setTypeName(BillTypeEnum.CTBI2.getName());
		expenseFees.add(expenseTypeResp4);
		ExpenseTypeResp expenseTypeResp5 = new ExpenseTypeResp();
		expenseTypeResp5.setTypeCode(BillTypeEnum.CTBI1.getCode());
		expenseTypeResp5.setTypeName(BillTypeEnum.CTBI1.getName());
		expenseFees.add(expenseTypeResp5);
		ExpenseTypeResp expenseTypeResp6 = new ExpenseTypeResp();
		expenseTypeResp6.setTypeCode(BillTypeEnum.CTBI3.getCode());
		expenseTypeResp6.setTypeName(BillTypeEnum.CTBI3.getName());
		expenseFees.add(expenseTypeResp6);
		ExpenseTypeResp expenseTypeResp7 = new ExpenseTypeResp();
		expenseTypeResp7.setTypeCode(BillTypeEnum.CTBI5.getCode());
		expenseTypeResp7.setTypeName(BillTypeEnum.CTBI5.getName());
		expenseFees.add(expenseTypeResp7);
		ExpenseTypeResp expenseTypeResp8= new ExpenseTypeResp();
		expenseTypeResp8.setTypeCode(BillTypeEnum.CTBI6.getCode());
		expenseTypeResp8.setTypeName(BillTypeEnum.CTBI6.getName());
		expenseFees.add(expenseTypeResp8);
		ExpenseTypeResp expenseTypeResp10= new ExpenseTypeResp();
		expenseTypeResp10.setTypeCode(BillTypeEnum.CTBI7.getCode());
		expenseTypeResp10.setTypeName(BillTypeEnum.CTBI7.getName());
		expenseFees.add(expenseTypeResp10);
		resultMap.put("expenseFees", expenseFees);

		//通用授权码
		List<ExpenseTypeResp> commonFees = new ArrayList<ExpenseTypeResp>();
		commonFees.add(expenseTypeResp4);
		commonFees.add(expenseTypeResp5);
		commonFees.add(expenseTypeResp6);
		commonFees.add(expenseTypeResp7);
		commonFees.add(expenseTypeResp8);
		commonFees.add(feeExpenseTypeResp2);
		commonFees.add(feeExpenseTypeResp3);
		commonFees.add(feeExpenseTypeResp4);
		commonFees.add(feeExpenseTypeResp);
		resultMap.put("commonFees", commonFees);
		return resultMap;
	}

	public String generateAPNo(String code, Integer maxAPCode)
	{
		String APSn = code + new DecimalFormat("00000000").format(maxAPCode);
		return APSn;
	}

	public void insertCoupon(Coupon coupon) throws LandseaException
	{
		try
		{
			coupon.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			Integer maxAPCode = couponReadMapper.getMaxCouponCode();
			String applicationCode = generateAPNo("AP", maxAPCode);
			coupon.setApplicationCode(applicationCode);

			Date startTime = coupon.getEffectiveStartTime();
			if (null != startTime)
			{
				// 格式化时间
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(startTime);
				startCalendar.set(Calendar.HOUR_OF_DAY, 0);
				startCalendar.set(Calendar.MINUTE, 0);
				startCalendar.set(Calendar.SECOND, 0);
				coupon.setEffectiveStartTime(startCalendar.getTime());
			}
			Date endTime = coupon.getEffectiveEndTime();
			if (null != endTime)
			{
				Calendar endCalendar = Calendar.getInstance();
				endCalendar.setTime(endTime);
				endCalendar.set(Calendar.HOUR_OF_DAY, 0);
				endCalendar.set(Calendar.MINUTE, 0);
				endCalendar.set(Calendar.SECOND, 0);
				coupon.setEffectiveEndTime(endCalendar.getTime());
			}

			coupon.setStatus(CouponStatus.DRAFT);
			if(AppropriateType.PERSION.equals(coupon.getAppropriateType()))
			{
				coupon.setAmount(1);
				// 查询最大的用户版本
				coupon.setVersionNumber(VariableChar.DIR+ customerVersionReadMapper.getCurrentMaxVersionNumber(coupon.getUserId()));
			}

			if (CodeType.RELIEFTYPE.equals(coupon.getCodeType()))
			{
				coupon.setOfferType(OfferType.BYSINGLE);
			}
			if (CodeType.OFFERTYPE.equals(coupon.getCodeType()) && FeeType.MONTHLYBILLCREATED.equals(coupon.getFeeType()))
			{
				coupon.setOfferType(OfferType.BYSINGLE);
			}

			if(null != coupon.getProjectIds())
			{
				String[] idArr = coupon.getProjectIds().split(",");
				String areas = "";
				List<String> areaList = new ArrayList<String>();
				for (String str : idArr)
				{
					Integer projectId = Integer.parseInt(str);
					String area = projectReadMapper.selectAreaById(projectId);
					if (StringUtils.isEmpty(area))
					{
						continue;
					}
					if (areaList.contains(area))
					{
						continue;
					}
					areaList.add(area);
					areas += ","+area; 
				}
				areas = areas.substring(1);
				coupon.setAreaIds(areas);
			}

			couponWriteMapper.insertSelective(coupon);
		}
		catch (Exception e)
		{
			LOG.error("insertCoupon|授权码新增报错"+coupon.getId());
			throw LandseaException.createException(AuthorizationResultCode.E00550002);
		}
	}

	@Override
	public void editCoupon(Coupon coupon) throws LandseaException
	{
		try
		{
			Date startTime = coupon.getEffectiveStartTime();
			if (null != startTime)
			{
				// 格式化时间
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(startTime);
				startCalendar.set(Calendar.HOUR_OF_DAY, 0);
				startCalendar.set(Calendar.MINUTE, 0);
				startCalendar.set(Calendar.SECOND, 0);
				coupon.setEffectiveStartTime(startCalendar.getTime());
			}
			Date endTime = coupon.getEffectiveEndTime();
			if (null != endTime)
			{
				Calendar endCalendar = Calendar.getInstance();
				endCalendar.setTime(endTime);
				endCalendar.set(Calendar.HOUR_OF_DAY, 0);
				endCalendar.set(Calendar.MINUTE, 0);
				endCalendar.set(Calendar.SECOND, 0);
				coupon.setEffectiveEndTime(endCalendar.getTime());
			}

			Integer id = coupon.getId();
			Coupon coupon1 = couponReadMapper.selectByPrimaryKey(id);
			if (null == coupon1 )
			{
				LOG.error("editCoupon|授权码不存在.couponId=" + id);
				throw LandseaException.createException(AuthorizationResultCode.E00550001);
			}

			//判断状态，只有草稿的可以被修改
			if (!CouponStatus.DRAFT.equals(coupon1.getStatus()))
			{
				LOG.error("editCoupon|授权码不允许修改.couponId=" + id);
				throw LandseaException.createException(AuthorizationResultCode.E00550006);
			}

			if(AppropriateType.PERSION.equals(coupon.getAppropriateType()))
			{
				coupon.setAmount(1);
				// 查询最大的用户版本
				coupon.setVersionNumber(VariableChar.DIR+ customerVersionReadMapper.getCurrentMaxVersionNumber(coupon.getUserId()));
			}

			coupon.setCreatedBy(coupon1.getCreatedBy());
			coupon.setCreatedDtm(coupon1.getCreatedDtm());
			coupon.setLastModifiedBy(SecurityUtils.getCurrentLogin().getUserId());
			coupon.setLastModifiedDtm( new Timestamp(new Date().getTime()));
			couponWriteMapper.updateByPrimaryKeySelective(coupon);   
		}
		catch (LandseaException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw LandseaException.createException(AuthorizationResultCode.E00550004);
		}
	}

	@Override
	public void deleteCoupon(Integer id)throws LandseaException
	{
		try
		{
			Coupon coupon = couponReadMapper.selectByPrimaryKey(id);
			if (null == coupon)
			{
				LOG.error("deleteCoupon|授权码不存在.id=" + id);
				throw LandseaException.createException(AuthorizationResultCode.E00550001);
			}
			//只有草稿的可以被删除 
			if (!CouponStatus.DRAFT.equals(coupon.getStatus()))
			{
				LOG.error("editCoupon|授权码不允许删除.couponId=" + id);
				throw LandseaException.createException(AuthorizationResultCode.E00550007);
			}
			coupon.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			couponWriteMapper.deleteByPrimaryKey(coupon);
		}
		catch (LandseaException e) 
		{
			throw e;
		}
		catch (Exception e)
		{
			LOG.error("deleteCoupon|授权码删除错误.id=" + id);
			throw LandseaException.createException(AuthorizationResultCode.E00550005);
		}

	}

	@Override
	public List<CouponPersonalDTO> queryPersonByPage(QueryUtil queryUtil)
	{
		List<CouponPersonalDTO> dtoList = new ArrayList<CouponPersonalDTO>();
		try
		{
			//查出登陆人的授权项目
			List<AuthorizedItemResp> authorizedProjectResps = projectReadMapper.selectProjectAndCity(SecurityUtils.getCurrentLogin().getUserId());
			List<Integer> prjIdLists = new ArrayList<Integer>();
			for (AuthorizedItemResp authorizedItemResp : authorizedProjectResps)
			{
				prjIdLists.add(authorizedItemResp.getPrjId());
			}

			//查出登陆人的授权区域
			List<AuthorizedItemResp> authorizedAreaResps =projectReadMapper.selectAreaByUserId(SecurityUtils.getCurrentLogin().getUserId());
			List<String> areaLists = new ArrayList<String>();
			for (AuthorizedItemResp authorizedItemResp : authorizedAreaResps)
			{
				areaLists.add(authorizedItemResp.getCityCode());
			}
			//授权码sql拼接
			//拼接门店
			LOG.info("query personal coupon start:"+new Date());
			String projectSql = buildProjectSql(areaLists,prjIdLists);
			LOG.info("query personal coupon start 2:"+new Date());
			queryUtil.getParams().put("authItemCondition", projectSql);
			queryUtil.getParams().put("userId", SecurityUtils.getCurrentLogin().getUserId());
			dtoList = couponReadMapper.selectPersonByPage(queryUtil);
			LOG.info("query personal coupon end:"+new Date());
			for (CouponPersonalDTO couponPersonalDTO : dtoList)
			{
				if(null != couponPersonalDTO.getProjectIds())
				{
					String[] prjIds = couponPersonalDTO.getProjectIds().split(",");
					List<Integer> projectIds = new ArrayList<Integer>();
					for (String str : prjIds)
					{
						if (StringUtils.isEmpty(str))
						{
							continue;
						}
						if (projectIds.contains(str))
						{
							continue;
						}
						Integer projectId = Integer.valueOf(str);
						projectIds.add(projectId);
					}
					List<String> projectNames = projectReadMapper.selectProjectNameArray(projectIds);
					String projectName = new String();
					if (CollectionUtils.isNotEmpty(projectNames)) 
					{
						for (String str : projectNames)
						{
							projectName += ","+str;      
						}
						projectName = projectName.substring(1);
						couponPersonalDTO.setProjectName(projectName); 
					}
				}
				if (null != couponPersonalDTO.getAreaIds())
				{
					String[] areas = couponPersonalDTO.getAreaIds().split(",");
					List<String> areaNames = codeReadMapper.selectCodeArray(areas);
					String areaName = new String();
					List<String> areaList = new ArrayList<String>();
					if (CollectionUtils.isNotEmpty(areaNames)) 
					{
						for (String str : areaNames)
						{
							if (StringUtils.isEmpty(str))
							{
								continue;
							}
							if (areaList.contains(str))
							{
								continue;
							}
							areaList.add(str);
							areaName += ","+str; 
						}
						areaName = areaName.substring(1);
						couponPersonalDTO.setAreaName(areaName);
					}
				}
			}
			LOG.info("query personal coupon end 2:"+new Date());
			//authCodeService.filterPersonExpireCoupon(dtoList, false);

		}
		catch (Exception e)
		{
		}
		return dtoList;
	}

	private String buildProjectSql(List<String> areaLists,List<Integer> prjIdLists)
	{
		if (CollectionUtils.isEmpty(areaLists))
		{
			return null;
		}
		StringBuilder stringBuilder = new StringBuilder();
		if (CollectionUtils.isEmpty(prjIdLists))
		{
			stringBuilder.append(" and( ");
			int size = areaLists.size();       
			for (int i = 0 ; i < size; i ++)
			{
				stringBuilder.append("(FIND_IN_SET('");
				stringBuilder.append(areaLists.get(i));
				stringBuilder.append("',lsc.area_ids) >=1 and lsc.range_type = 'AREA' )");

				if (i != size - 1)
				{
					stringBuilder.append(" or ");
				}
			}
			stringBuilder.append(" )");
		}
		else
		{
			stringBuilder.append(" and( (");
			int areaSize = areaLists.size();       
			for (int i = 0 ; i < areaSize; i ++)
			{
				stringBuilder.append("(FIND_IN_SET('");
				stringBuilder.append(areaLists.get(i));
				stringBuilder.append("',lsc.area_ids) >=1 and lsc.range_type = 'AREA' )");

				if (i != areaSize - 1)
				{
					stringBuilder.append(" or ");
				}
			}
			stringBuilder.append(" ) or ( ");
			int projectSize = prjIdLists.size();       
			for (int i = 0 ; i < projectSize; i ++)
			{
				stringBuilder.append("(FIND_IN_SET(");
				stringBuilder.append(prjIdLists.get(i));
				stringBuilder.append(",lsc.project_ids) >=1 and lsc.range_type = 'PROJECT' )");
				if (i != projectSize - 1)
				{
					stringBuilder.append(" or ");
				}
			}
			stringBuilder.append(" ) )");
		}


		return stringBuilder.toString();
	}


	@Override
	public List<CouponGeneralDTO> queryGeneralByPage(QueryUtil queryUtil)
	{
		List<CouponGeneralDTO> dtoList = new ArrayList<CouponGeneralDTO>();
		try
		{
			//查出登陆人的授权项目
			List<AuthorizedItemResp> authorizedProjectResps = projectReadMapper.selectProjectAndCity(SecurityUtils.getCurrentLogin().getUserId());
			List<Integer> prjIdLists = new ArrayList<Integer>();
			for (AuthorizedItemResp authorizedItemResp : authorizedProjectResps)
			{
				prjIdLists.add(authorizedItemResp.getPrjId());
			}

			//查出登陆人的授权区域
			List<AuthorizedItemResp> authorizedAreaResps =projectReadMapper.selectAreaByUserId(SecurityUtils.getCurrentLogin().getUserId());
			List<String> areaLists = new ArrayList<String>();
			for (AuthorizedItemResp authorizedItemResp : authorizedAreaResps)
			{
				areaLists.add(authorizedItemResp.getCityCode());
			}
			//授权码sql拼接
			//拼接门店
			String projectSql = buildProjectSql(areaLists,prjIdLists);
			queryUtil.getParams().put("authItemCondition", projectSql);
			queryUtil.getParams().put("userId", SecurityUtils.getCurrentLogin().getUserId());
			dtoList = couponReadMapper.selectGeneralByPage(queryUtil);

			for (CouponGeneralDTO couponGeneralDTO : dtoList)
			{
				if (null != couponGeneralDTO.getProjectIds())
				{ 
					String[] idArr = couponGeneralDTO.getProjectIds().split(",");
					List<Integer> projectIds = new ArrayList<Integer>();
					for (String str : idArr)
					{
						if (StringUtils.isEmpty(str))
						{
							continue;
						}
						if (projectIds.contains(str))
						{
							continue;
						}
						Integer projectId = Integer.valueOf(str);
						projectIds.add(projectId);
					}
					List<Project> projects = projectReadMapper.selectPrjArray(projectIds);
					String projectName = new String();
					List<Map<String, Object>> projectInfoList = new ArrayList<Map<String, Object>>();
					for (Project project : projects)
					{ 
						Map<String, Object> projectMap = new HashMap<String, Object>();  
						projectMap.put("projectId", project.getId());
						projectMap.put("prjName", project.getPrjName());
						projectInfoList.add(projectMap);
						projectName += ","+ project.getPrjName(); 
					}
					projectName = projectName.substring(1);
					couponGeneralDTO.setProjectName(projectName);
					couponGeneralDTO.setProjectInfoList(projectInfoList);
				}
				if (null != couponGeneralDTO.getAreaIds())
				{
					String[] areaIds = couponGeneralDTO.getAreaIds().split(",");
					List<String> areaNames = codeReadMapper.selectCodeArray(areaIds);
					List<String> areaList = new ArrayList<String>();
					String areaName = new String();
					if (CollectionUtils.isNotEmpty(areaNames)) 
					{
						for (String str : areaNames)
						{
							if (StringUtils.isEmpty(str))
							{
								continue;
							}
							if (areaList.contains(str))
							{
								continue;
							}
							areaList.add(str);
							areaName += ","+str; 
						}
						areaName = areaName.substring(1);
						couponGeneralDTO.setAreaName(areaName);
					}
					
				}
			}
			//authCodeService.filterGeneralExpireCoupon(dtoList, false);
		}
		catch (Exception e)
		{

		}
		return dtoList;
	}

	@Override
	public CouponDTO queryCoupon(Integer id)
	{
		CouponDTO coupon = new CouponDTO();
		try
		{               
			coupon = couponReadMapper.queryByPrimaryKey(id); 
			if (null != coupon.getAreaIds())
			{

				List<ExpenseTypeResp> areaLists = new ArrayList<ExpenseTypeResp>();
				String[] areaStr = coupon.getAreaIds().split(",");
				String areaName = new String();
				for (String str : areaStr)
				{
					areaName = balanceSheetRecordReadMapper.selectExpenditure(str);
					ExpenseTypeResp resp = new ExpenseTypeResp();
					resp.setTypeCode(str);
					resp.setTypeName(areaName);
					areaLists.add(resp);
					coupon.setAreaList(areaLists);
				}

				if (null != coupon.getProjectIds())
				{
					List<ProjectResp> projectLists = new ArrayList<ProjectResp>();
					String projectName  = new String();

					String[] projectIdStr = coupon.getProjectIds().split(",");
					for (String str : projectIdStr)
					{
						ProjectResp projectResp = new ProjectResp();
						Integer projectId = Integer.valueOf(str);
						Project project =  projectReadMapper.selectByPrimaryKey(projectId);
						projectName  =project.getPrjName();  
						projectResp.setPrjId(projectId);
						projectResp.setPrjName(projectName);
						projectLists.add(projectResp);
						coupon.setProjectList(projectLists);
					}
				}

			}
		}
		catch (Exception e)
		{
		}
		return coupon;
	}


	@Override
	public  List<CouponPutRecordResp> queryCouponPutRecord(QueryUtil queryUtil)
	{
		List<CouponPutRecordResp> recordResps = new ArrayList<CouponPutRecordResp>();
		try{
			recordResps = couponPutRecordReadMapper.queryCouponPutRecord(queryUtil);
		}
		catch (Exception e) {    
		}
		return recordResps;
	}

	@Override
	public List<CouponUsedResp> queryCouponUsedrecord(QueryUtil queryUtil)
	{
		List<CouponUsedResp> recordResps = new ArrayList<CouponUsedResp>();
		try{
			recordResps = coupUsedRecordReadMapper.queryCouponUsedrecord(queryUtil);
		}
		catch (Exception e) {

		}
		return recordResps;
	}

	@Override
	public void insertCouponPutRecord(CouponPutRecordReq req)
			throws LandseaException
	{
		try{
			CouponPutRecord record = new CouponPutRecord();
			record.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
			record.setPutBy(SecurityUtils.getCurrentLogin().getUserId());
			record.setCouponId(req.getCouponId());
			Date effectiveStartTime = DateUtil.convertDate(req.getEffectiveStartTime());
			Date effectiveEndTime = DateUtil.convertDate(req.getEffectiveEndTime());
			record.setEffectiveStartTime(effectiveStartTime);
			record.setEffectiveEndTime(effectiveEndTime);

			Coupon coupon = couponReadMapper.selectByPrimaryKey(req.getCouponId());
			if(null == coupon){
				LOG.error("PutCoupon|投放授权码异常.couponId="+req.getCouponId() );
				throw LandseaException.createException(AuthorizationResultCode.E00550001); 
			}
			else 
			{
				if (!ApprovalStatus.APASS.equals(coupon.getApprovalStatus()))
				{
					LOG.error("PutCoupon|投放授权码异常.couponId="+req.getCouponId() );
					throw LandseaException.createException(AuthorizationResultCode.E005500010);
				}
				//总数量
				Integer amount = coupon.getAmount();
				//已经投放过的数量
				Integer putOnAmount = couponReadMapper.selectByCouponId(req.getCouponId());

				//本次投放数量
				Integer putAmount = req.getPutAmount();
				//还没有投放过授权码
				if (null == putOnAmount)
				{
					putOnAmount = 0; 
					if (putAmount.intValue() < amount.intValue() )
					{
						coupon.setStatus(CouponStatus.PARTPUTON);
						coupon.setPutOnAmount(putAmount);
						couponWriteMapper.updateByPrimaryKeySelective(coupon); 
					}
					else if (putAmount.intValue() == amount.intValue())
					{
						coupon.setStatus(CouponStatus.PUTON);
						coupon.setPutOnAmount(putAmount);
						couponWriteMapper.updateByPrimaryKeySelective(coupon); 
					}
					else if (putAmount.intValue() > amount.intValue())
					{
						LOG.error("PutCoupon|投放授权码异常.couponId="+req.getCouponId() );
						throw LandseaException.createException(AuthorizationResultCode.E005500011);   
					}

				}

				//已经投放过授权码
				else
				{
					Integer amounts = (int)(Arith.add(putAmount,putOnAmount));
					if(amounts.intValue() > amount.intValue())
					{
						LOG.error("PutCoupon|投放授权码异常.couponId="+req.getCouponId() );
						throw LandseaException.createException(AuthorizationResultCode.E005500011);
					}
					else if (amounts.intValue() < amount.intValue())
					{
						coupon.setStatus(CouponStatus.PARTPUTON);
						coupon.setPutOnAmount(amounts);
						couponWriteMapper.updateByPrimaryKeySelective(coupon); 
					}
					else if (amounts.intValue() == amount.intValue())
					{
						coupon.setStatus(CouponStatus.PUTON);
						coupon.setPutOnAmount(amounts);
						couponWriteMapper.updateByPrimaryKeySelective(coupon); 
					}
				}

				record.setPutAmount(putAmount);
				record.setUsedAmount(0);
				couponPutRecordWriteMapper.insertSelective(record);
			}
		}
		catch (LandseaException e) 
		{
			throw e;
		}
		catch (Exception e)
		{
			LOG.error("PutCoupon|投放授权码异常.couponId="+req.getCouponId(), e);
			throw LandseaException.createException(AuthorizationResultCode.E00550009, e);
		}
	}

	@Override
	public Map<String, Object> getAuthCodeList()
	{
		Map<String,Object> respMap = new HashMap<String,Object>();
		Integer userId = SecurityUtils.getCurrentLogin().getUserId();

		List<CouponDTO> usedCouponList = couponReadMapper.selectByUserIdAndStatus(userId,DepositEnum._DS3.getCode());
		if ( CollectionUtils.isNotEmpty(usedCouponList))
		{   
			//已使用
			List<WeChatAuthCodeResp> used = setWeChatOfAuthCode(usedCouponList);
			//未使用
			List<CouponDTO> commonCouponList = couponReadMapper.selectCommonAuthCode();
			List<CouponDTO> unUsedCouponList = new ArrayList<CouponDTO>();
			List<Integer> usedCouponIdList = new ArrayList<>();
			if (CollectionUtils.isNotEmpty(commonCouponList))
			{
				for (CouponDTO couponDTO : usedCouponList)
				{
					usedCouponIdList.add(couponDTO.getId());
				}
			}

			if (CollectionUtils.isNotEmpty(usedCouponIdList))
			{
				for (CouponDTO couponDTO : commonCouponList)
				{
					if (usedCouponIdList.contains(couponDTO.getId()))
					{
						continue;
					}
					unUsedCouponList.add(couponDTO);
				}
			}
			List<WeChatAuthCodeResp> unUsed = setWeChatOfAuthCode(unUsedCouponList);
			respMap.put("unUsed", unUsed);
			respMap.put("used", used);
		}
		//新用户
		else{
			List<CouponDTO> commonCouponList = couponReadMapper.selectCommonAuthCode();
			List<WeChatAuthCodeResp> unUsed = setWeChatOfAuthCode(commonCouponList);
			respMap.put("unUsed", unUsed);
			respMap.put("used", "");
		}
		return respMap;
	}

	private List<WeChatAuthCodeResp> setWeChatOfAuthCode(List<CouponDTO> CouponDTOList)
	{
		List<WeChatAuthCodeResp> unUsed = new ArrayList<WeChatAuthCodeResp>();
		for (CouponDTO unUsedCoupon : CouponDTOList)
		{
			WeChatAuthCodeResp unUsedResp = new WeChatAuthCodeResp();
			List<String> authCodeDetailList = new ArrayList<String>();
			unUsedResp.setQuota(unUsedCoupon.getQuota());
			unUsedResp.setMoneyType(unUsedCoupon.getMoneyType());
			unUsedResp.setEffectiveStartTime(DateUtil.convertDate(unUsedCoupon.getEffectiveStartTime()));
			unUsedResp.setEffectiveEndTime(DateUtil.convertDate(unUsedCoupon.getEffectiveEndTime()));
			if (BillTypeEnum.CTBI1.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI1.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI2.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI2.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI3.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI3.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI4.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI4.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI5.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI5.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI6.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI6.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI7.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI7.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI8.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI8.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI9.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI9.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI10.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI10.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI11.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI11.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI12.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI12.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI13.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI13.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI14.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI14.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI15.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI15.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI16.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI16.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI17.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI17.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI18.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI18.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI19.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI19.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI20.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI20.getName()+"优惠券");
			}
			else if (BillTypeEnum.CTBI21.getCode().equals(unUsedCoupon.getAuthFeeCode()))
			{
				unUsedResp.setFeeType(BillTypeEnum.CTBI21.getName()+"优惠券");
			}

			String comment1 = null;
			if(OfferType.BYMONTH.equals(unUsedCoupon.getOfferTypeCode()))
			{
				comment1 = "此券为每月使用优惠券";
			}
			else if (OfferType.BYSINGLE.equals(unUsedCoupon.getOfferTypeCode()))
			{
				comment1 = "此券为单次使用优惠券";
			}

			String comment2 = null;
			if (null != unUsedCoupon.getProjectIds())
			{
				String [] str = unUsedCoupon.getProjectIds().split(",");
				String projectNames = new String();
				for (String prjId : str)
				{
					Integer projectId = Integer.valueOf(prjId);
					Project project =  projectReadMapper.selectByPrimaryKey(projectId);
					String projectName  =project.getPrjName();   
					projectNames += ","+projectName;      
				}
				comment2 = "此券可在"+projectNames.substring(1)+"使用";
			}
			else
			{
				String [] str = unUsedCoupon.getAreaIds().split(",");
				String areaNames = new String();
				for (String area : str)
				{

					String areaName = balanceSheetRecordReadMapper.selectExpenditure(area);
					areaNames += ","+areaName; 
				}
				comment2 = "此券可在"+areaNames.substring(1)+"所有门店使用";
			}
			String comment3 = null;
			if (MoneyType.DISCOUNT.equals(unUsedCoupon.getMoneyType()))
			{
				comment3 = "此券折扣为最低使用折扣，实际使用折扣以账单为准";
			}
			else
			{
				comment3 = "此券金额为最大使用金额，实际使用金额以账单为准";
			}
			authCodeDetailList.add(comment1);
			authCodeDetailList.add(comment2);
			authCodeDetailList.add(comment3);
			unUsedResp.setAuthCodeDetailList(authCodeDetailList);  
			unUsed.add(unUsedResp);
		}
		return unUsed;
	}
}
